import Vue from 'vue'
import VueRouter from 'vue-router'
import Common_login from "@/views/Common_Login.vue";
import A_P_AirQualitySensor from "@/views/A_P_AirQualitySensor.vue";
import A_P_InfraredSensor from "@/views/A_P_InfraredSensor.vue";
import A_P_LightSensor from "@/views/A_P_LightSensor.vue";
import A_P_TemperatureHumiditySensor from "@/views/A_P_TemperatureHumiditySensor.vue";
import Common_Balcony from "@/views/Common_Balcony";
import Common_Bedroom from "@/views/Common_Bedroom";
import Common_Kitchen from "@/views/Common_Kitchen";
import Common_LivingRoom from "@/views/Common_LivingRoom";
import Common_Layout from "@/views/Common_Layout";
import A_UsersManagement from "@/views/A_UsersManagement";
import Common_Devices from "@/views/Common_Devices";
import A_Log from "@/views/A_Log";
import echartsTest from "@/views/echartsTest";
import A_P_CoSensor from "@/views/A_P_CoSensor";
import A_P_FlameSensor from "@/views/A_P_FlameSensor";

Vue.use(VueRouter)

const routes = [

    {
    path: '/login',  //登陆界面，parent,child公用
    name: 'Common_Login',
    component: Common_login
  },
  {
    path: '/',  //进入layout
    name: 'Common_Layout',
    component: Common_Layout,
    children:[
      {
        path: '/A_P_AirQualitySensor',    //进入空气质量传感器信息界面，仅parent模式下
        name: 'A_P_AQS',
        component: A_P_AirQualitySensor
      },
      {
        path: '/A_P_CoSensor',    //进入一氧化碳传感器信息界面，仅parent模式下
        name: 'A_P_CoS',
        component: A_P_CoSensor
      },
      {
        path: '/A_P_FlameSensor',    //进入火焰传感器信息界面，仅parent模式下
        name: 'A_P_FS',
        component: A_P_FlameSensor
      },
      {
        path: '/A_P_InfraredSensor',    //进入人体红外传感器信息界面，仅parent模式下
        name: 'A_P_IS',
        component: A_P_InfraredSensor
      },
      {
        path: '/A_P_LightSensor',   //进入光敏传感器信息界面，仅parent模式下
        name: 'A_P_LS',
        component: A_P_LightSensor
      },
      {
        path: '/A_P_TemperatureHumiditySensor',   //进入温湿度传感器信息界面，仅parent模式下
        name: 'A_P_THS',
        component: A_P_TemperatureHumiditySensor
      },
      {
        path: '/A_UsersManagement',
        name: 'A_UM',
        component: A_UsersManagement
      },
      {
        path: '/A_Log',   //进入光敏传感器信息界面，仅parent模式下
        name: 'A_Log',
        component: A_Log
      },
      {
        path: '/Common_Devices',   //进入之空调设备信息界面，parent和child都可查看
        name: 'C_Dev',
        component: Common_Devices
      },
      {
        path: '/Common_Balcony',   //这是阳台环境信息界面，parent和child都可查看
        name: 'C_Balcony',
        component: Common_Balcony
      },
      {
        path: '/Common_Bedroom',   //进入之卧室环境信息界面，parent和child都可查看
        name: 'C_Bedroom',
        component: Common_Bedroom
      },
      {
        path: '/Common_Kitchen',   //进入之厨房信息界面，parent和child都可查看
        name: 'C_Kitchen',
        component: Common_Kitchen
      },
      {
        path: '/Common_LivingRoom',   //进入之客厅环境信息界面，parent和child都可查看
        name: 'C_LivingRoom',
        component: Common_LivingRoom
      },
      {
        path: '/echartsTest',
        name: 'echartsTest',
        component: echartsTest
      },
    ]
  },


]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

/*
 *路由守卫不安全，前端的数据可以篡改，jwt登录鉴权放在服务器上完成
 *  */
//路由守卫
router.beforeEach((to, from, next) =>{
  //如果进入的使登陆界面，允许进入
  if(to.path === '/login'){
    next();
  }
  //获取登录用户信息
  const user = localStorage.getItem("user");
  //如果无用户登录，或者进入的不是登录界面，则拦截，进入登陆界面
  if(!user && to.path !== '/login'){
    return next("/login");
  }
  next();
})

export default router
